package com.ruoyi.system.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.nacos.api.config.ConfigService;
import com.ruoyi.common.security.annotation.InnerAuth;
import com.ruoyi.system.domain.Activity;
import com.ruoyi.system.service.IActivityService;
import com.ruoyi.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.domain.Carousel;
import com.ruoyi.system.service.ICarouselService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 首页轮播图增删改查Controller
 * 
 * @author hyb
 * @date 2023-02-21
 */
@RestController
@RequestMapping("/carousel")
public class CarouselController extends BaseController
{
    @Autowired
    private ICarouselService carouselService;

    @Autowired
    private ISysConfigService configService;

    @Autowired
    private IActivityService activityService;

    @InnerAuth
    @GetMapping("/listCarousel")
    public AjaxResult listCarousel()
    {
        String limit=configService.selectConfigByKey("sys.carousel.showNumber");
        List<Carousel> list = carouselService.listCarousel(Long.parseLong(limit));
        return AjaxResult.success().put("list",list);
    }

    /**
     * 查询首页轮播图增删改查列表
     */
    @RequiresPermissions("carousel:carousel:list")
    @GetMapping("/list")
    public TableDataInfo list(Carousel carousel)
    {
        startPage();
        List<Carousel> list = carouselService.selectCarouselList(carousel);
        return getDataTable(list);
    }

    /**
     * 导出首页轮播图增删改查列表
     */
    @RequiresPermissions("carousel:carousel:export")
    @Log(title = "首页轮播图增删改查", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Carousel carousel)
    {
        List<Carousel> list = carouselService.selectCarouselList(carousel);
        ExcelUtil<Carousel> util = new ExcelUtil<Carousel>(Carousel.class);
        util.exportExcel(response, list, "首页轮播图增删改查数据");
    }

    /**
     * 获取首页轮播图增删改查详细信息
     */
    @RequiresPermissions("carousel:carousel:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(carouselService.selectCarouselById(id));
    }

    /**
     * 新增首页轮播图增删改查
     */
    @RequiresPermissions("carousel:carousel:add")
    @Log(title = "首页轮播图增删改查", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Carousel carousel)
    {
        Activity activity = new Activity();
        activity.setNumber(String.valueOf(carousel.getActivityNumber()));
        List<Activity> list = activityService.selectActivityList(activity);
        if(list.size()==0){
            return AjaxResult.error("该活动不存在,不能添加轮播图");
        }
        carousel.setPf1(String.valueOf(list.get(0).getId()));
        return toAjax(carouselService.insertCarousel(carousel));
    }

    /**
     * 修改首页轮播图增删改查
     */
    @RequiresPermissions("carousel:carousel:edit")
    @Log(title = "首页轮播图增删改查", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Carousel carousel)
    {
        if(carousel.getActivityNumber()!=null){
            Activity activity = new Activity();
            activity.setNumber(String.valueOf(carousel.getActivityNumber()));
            List<Activity> list = activityService.selectActivityList(activity);
            if(list.size()==0){
                return AjaxResult.error("该活动不存在,不能添加轮播图");
            }
            carousel.setPf1(String.valueOf(list.get(0).getId()));
        }

        return toAjax(carouselService.updateCarousel(carousel));
    }

    /**
     * 删除首页轮播图增删改查
     */
    @RequiresPermissions("carousel:carousel:remove")
    @Log(title = "首页轮播图增删改查", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(carouselService.deleteCarouselByIds(ids));
    }
}
